###This produces the tabular results for party reputations of traits in Replication Data for: Disputed Ownership: Parties, Issues, and Traits in the Minds of Voters by Stephen N. Goggin and Alexander G. Theodoridis

##Need to set this:
setwd("~/Desktop/DisputedOwnership_Replication")

library(foreign)
library(car)
library(boot)
library(bootstrap)
library(survey)

indu <- read.csv("trait_reputation.csv")
attach(indu)

#pid3lean -> 1 = Dem; 2 = Rep; 3 = Ind
pid3lean <- recode(pid7,"1 = -1; 2 = -1; 3 = -1; 4 = 0; 5 = 1; 6 = 1; 7 = 1; 8 = NA")

##Creating Trait Variables
D_Comp <- as.numeric(PartyTrait_Dem_com)
D_Mora <- as.numeric(PartyTrait_Dem_mor)
D_Lead <- as.numeric(PartyTrait_Dem_str)
D_Empa <- as.numeric(PartyTrait_Dem_car)
D_Know <- as.numeric(PartyTrait_Dem_kno)
D_Gree <- as.numeric(PartyTrait_Dem_gre)
D_Inde <- as.numeric(PartyTrait_Dem_ind)
D_Hard <- as.numeric(PartyTrait_Dem_har)
D_Hone <- as.numeric(PartyTrait_Dem_hon)
D_Insp <- as.numeric(PartyTrait_Dem_ins)

R_Comp <- as.numeric(PartyTrait_Rep_com)
R_Mora <- as.numeric(PartyTrait_Rep_mor)
R_Lead <- as.numeric(PartyTrait_Rep_str)
R_Empa <- as.numeric(PartyTrait_Rep_car)
R_Know <- as.numeric(PartyTrait_Rep_kno)
R_Gree <- as.numeric(PartyTrait_Rep_gre)
R_Inde <- as.numeric(PartyTrait_Rep_ind)
R_Hard <- as.numeric(PartyTrait_Rep_har)
R_Hone <- as.numeric(PartyTrait_Rep_hon)
R_Insp <- as.numeric(PartyTrait_Rep_ins)

D_Comp[D_Comp==8 | D_Comp==9] <- NA
D_Mora[D_Mora==8 | D_Mora==9] <- NA
D_Lead[D_Lead==8 | D_Lead==9] <- NA
D_Empa[D_Empa==8 | D_Empa==9] <- NA
D_Know[D_Know==8 | D_Know==9] <- NA
D_Gree[D_Gree==8 | D_Gree==9] <- NA
D_Inde[D_Inde==8 | D_Inde==9] <- NA
D_Hard[D_Hard==8 | D_Hard==9] <- NA
D_Hone[D_Hone==8 | D_Hone==9] <- NA
D_Insp[D_Insp==8 | D_Insp==9] <- NA

R_Comp[R_Comp==8 | R_Comp==9] <- NA
R_Mora[R_Mora==8 | R_Mora==9] <- NA
R_Lead[R_Lead==8 | R_Lead==9] <- NA
R_Empa[R_Empa==8 | R_Empa==9] <- NA
R_Know[R_Know==8 | R_Know==9] <- NA
R_Gree[R_Gree==8 | R_Gree==9] <- NA
R_Inde[R_Inde==8 | R_Inde==9] <- NA
R_Hard[R_Hard==8 | R_Hard==9] <- NA
R_Hone[R_Hone==8 | R_Hone==9] <- NA
R_Insp[R_Insp==8 | R_Insp==9] <- NA

D_Comp <- D_Comp + -4
D_Mora <- D_Mora + -4
D_Lead <- D_Lead + -4
D_Empa <- D_Empa + -4
D_Know <- D_Know + -4
D_Gree <- D_Gree + -4
D_Inde <- D_Inde + -4
D_Hard <- D_Hard + -4
D_Hone <- D_Hone + -4
D_Insp <- D_Insp + -4

R_Comp <- R_Comp + -4
R_Mora <- R_Mora + -4
R_Lead <- R_Lead + -4
R_Empa <- R_Empa + -4
R_Know <- R_Know + -4
R_Gree <- R_Gree + -4
R_Inde <- R_Inde + -4
R_Hard <- R_Hard + -4
R_Hone <- R_Hone + -4
R_Insp <- R_Insp + -4

####################
#Create Data Frame with Essential Variables
indu_ready <- data.frame(caseid,weight,pid3lean,D_Comp,D_Mora,D_Lead,D_Empa,D_Know,D_Hard,D_Hone,D_Insp,R_Comp,R_Mora,R_Lead,R_Empa,R_Know,R_Hard,R_Hone,R_Insp)

#Creating Vectors for Output
MDiff_Comp <- NULL
MDiff_Mora <- NULL
MDiff_Lead <- NULL
MDiff_Empa <- NULL
MDiff_Know <- NULL
MDiff_Hard <- NULL
MDiff_Hone <- NULL
MDiff_Insp <- NULL

####################
#Bootstrapping it all

for (i in 1:10000){
	
#With weights
tempdata <- sample(1:nrow(indu_ready),nrow(indu_ready),replace=T,prob=indu_ready$weight)

partisans <- indu_ready[tempdata,]

Dem <- subset(partisans, partisans$pid3lean==-1)
Rep <- subset(partisans, partisans$pid3lean==1)	

D_D_Comp <- mean(Dem$D_Comp, na.rm=T)
D_R_Comp <- mean(Dem$R_Comp, na.rm=T)
R_R_Comp <- mean(Rep$R_Comp, na.rm=T)
R_D_Comp <- mean(Rep$D_Comp, na.rm=T)

D_D_Mora <- mean(Dem$D_Mora, na.rm=T)
D_R_Mora <- mean(Dem$R_Mora, na.rm=T)
R_R_Mora <- mean(Rep$R_Mora, na.rm=T)
R_D_Mora <- mean(Rep$D_Mora, na.rm=T)

D_D_Lead <- mean(Dem$D_Lead, na.rm=T)
D_R_Lead <- mean(Dem$R_Lead, na.rm=T)
R_R_Lead <- mean(Rep$R_Lead, na.rm=T)
R_D_Lead <- mean(Rep$D_Lead, na.rm=T)

D_D_Empa <- mean(Dem$D_Empa, na.rm=T)
D_R_Empa <- mean(Dem$R_Empa, na.rm=T)
R_R_Empa <- mean(Rep$R_Empa, na.rm=T)
R_D_Empa <- mean(Rep$D_Empa, na.rm=T)

D_D_Know <- mean(Dem$D_Know, na.rm=T)
D_R_Know <- mean(Dem$R_Know, na.rm=T)
R_R_Know <- mean(Rep$R_Know, na.rm=T)
R_D_Know <- mean(Rep$D_Know, na.rm=T)

D_D_Hard <- mean(Dem$D_Hard, na.rm=T)
D_R_Hard <- mean(Dem$R_Hard, na.rm=T)
R_R_Hard <- mean(Rep$R_Hard, na.rm=T)
R_D_Hard <- mean(Rep$D_Hard, na.rm=T)

D_D_Hone <- mean(Dem$D_Hone, na.rm=T)
D_R_Hone <- mean(Dem$R_Hone, na.rm=T)
R_R_Hone <- mean(Rep$R_Hone, na.rm=T)
R_D_Hone <- mean(Rep$D_Hone, na.rm=T)

D_D_Insp <- mean(Dem$D_Insp, na.rm=T)
D_R_Insp <- mean(Dem$R_Insp, na.rm=T)
R_R_Insp <- mean(Rep$R_Insp, na.rm=T)
R_D_Insp <- mean(Rep$D_Insp, na.rm=T)

D_Comp <- D_D_Comp - D_R_Comp
R_Comp <- R_R_Comp - R_D_Comp
D_Mora <- D_D_Mora - D_R_Mora
R_Mora <- R_R_Mora - R_D_Mora
D_Lead <- D_D_Lead - D_R_Lead
R_Lead <- R_R_Lead - R_D_Lead
D_Empa <- D_D_Empa - D_R_Empa
R_Empa <- R_R_Empa - R_D_Empa
D_Know <- D_D_Know - D_R_Know
R_Know <- R_R_Know - R_D_Know
D_Hard <- D_D_Hard - D_R_Hard
R_Hard <- R_R_Hard - R_D_Hard
D_Hone <- D_D_Hone - D_R_Hone
R_Hone <- R_R_Hone - R_D_Hone
D_Insp <- D_D_Insp - D_R_Insp
R_Insp <- R_R_Insp - R_D_Insp

Diff_Comp <- D_Comp - R_Comp
Diff_Mora <- D_Mora - R_Mora
Diff_Lead <- D_Lead - R_Lead
Diff_Empa <- D_Empa - R_Empa
Diff_Know <- D_Know - R_Know
Diff_Hard <- D_Hard - R_Hard
Diff_Hone <- D_Hone - R_Hone
Diff_Insp <- D_Insp - R_Insp

Diffs <- c(Diff_Comp,Diff_Mora,Diff_Lead,Diff_Empa,Diff_Know,Diff_Hard,Diff_Hone,Diff_Insp)

Mean_Diff <- mean(Diffs)

MDiff_Comp <- c(MDiff_Comp, Diff_Comp - Mean_Diff)
MDiff_Mora <- c(MDiff_Mora, Diff_Mora - Mean_Diff)
MDiff_Lead <- c(MDiff_Lead, Diff_Lead - Mean_Diff)
MDiff_Empa <- c(MDiff_Empa, Diff_Empa - Mean_Diff)
MDiff_Know <- c(MDiff_Know, Diff_Know - Mean_Diff)
MDiff_Hard <- c(MDiff_Hard, Diff_Hard - Mean_Diff)
MDiff_Hone <- c(MDiff_Hone, Diff_Hone - Mean_Diff)
MDiff_Insp <- c(MDiff_Insp, Diff_Insp - Mean_Diff)
		
}
	


####################
#Calculating the Actual Differences in Sample (Unweighted)

Dem <- subset(indu_ready, indu_ready$pid3lean==-1)
Rep <- subset(indu_ready, indu_ready$pid3lean==1)	

D_D_Comp <- mean(Dem$D_Comp, na.rm=T)
D_R_Comp <- mean(Dem$R_Comp, na.rm=T)
R_R_Comp <- mean(Rep$R_Comp, na.rm=T)
R_D_Comp <- mean(Rep$D_Comp, na.rm=T)

D_D_Mora <- mean(Dem$D_Mora, na.rm=T)
D_R_Mora <- mean(Dem$R_Mora, na.rm=T)
R_R_Mora <- mean(Rep$R_Mora, na.rm=T)
R_D_Mora <- mean(Rep$D_Mora, na.rm=T)

D_D_Lead <- mean(Dem$D_Lead, na.rm=T)
D_R_Lead <- mean(Dem$R_Lead, na.rm=T)
R_R_Lead <- mean(Rep$R_Lead, na.rm=T)
R_D_Lead <- mean(Rep$D_Lead, na.rm=T)

D_D_Empa <- mean(Dem$D_Empa, na.rm=T)
D_R_Empa <- mean(Dem$R_Empa, na.rm=T)
R_R_Empa <- mean(Rep$R_Empa, na.rm=T)
R_D_Empa <- mean(Rep$D_Empa, na.rm=T)

D_D_Know <- mean(Dem$D_Know, na.rm=T)
D_R_Know <- mean(Dem$R_Know, na.rm=T)
R_R_Know <- mean(Rep$R_Know, na.rm=T)
R_D_Know <- mean(Rep$D_Know, na.rm=T)

D_D_Hard <- mean(Dem$D_Hard, na.rm=T)
D_R_Hard <- mean(Dem$R_Hard, na.rm=T)
R_R_Hard <- mean(Rep$R_Hard, na.rm=T)
R_D_Hard <- mean(Rep$D_Hard, na.rm=T)

D_D_Hone <- mean(Dem$D_Hone, na.rm=T)
D_R_Hone <- mean(Dem$R_Hone, na.rm=T)
R_R_Hone <- mean(Rep$R_Hone, na.rm=T)
R_D_Hone <- mean(Rep$D_Hone, na.rm=T)

D_D_Insp <- mean(Dem$D_Insp, na.rm=T)
D_R_Insp <- mean(Dem$R_Insp, na.rm=T)
R_R_Insp <- mean(Rep$R_Insp, na.rm=T)
R_D_Insp <- mean(Rep$D_Insp, na.rm=T)

D_Comp <- D_D_Comp - D_R_Comp
R_Comp <- R_R_Comp - R_D_Comp
D_Mora <- D_D_Mora - D_R_Mora
R_Mora <- R_R_Mora - R_D_Mora
D_Lead <- D_D_Lead - D_R_Lead
R_Lead <- R_R_Lead - R_D_Lead
D_Empa <- D_D_Empa - D_R_Empa
R_Empa <- R_R_Empa - R_D_Empa
D_Know <- D_D_Know - D_R_Know
R_Know <- R_R_Know - R_D_Know
D_Hard <- D_D_Hard - D_R_Hard
R_Hard <- R_R_Hard - R_D_Hard
D_Hone <- D_D_Hone - D_R_Hone
R_Hone <- R_R_Hone - R_D_Hone
D_Insp <- D_D_Insp - D_R_Insp
R_Insp <- R_R_Insp - R_D_Insp

Diff_Comp <- D_Comp - R_Comp
Diff_Mora <- D_Mora - R_Mora
Diff_Lead <- D_Lead - R_Lead
Diff_Empa <- D_Empa - R_Empa
Diff_Know <- D_Know - R_Know
Diff_Hard <- D_Hard - R_Hard
Diff_Hone <- D_Hone - R_Hone
Diff_Insp <- D_Insp - R_Insp

Diffs <- c(Diff_Comp,Diff_Mora,Diff_Lead,Diff_Empa,Diff_Know,Diff_Hard,Diff_Hone,Diff_Insp)

Mean_Diff <- mean(Diffs)

ADiff_Comp <- Diff_Comp - Mean_Diff
ADiff_Mora <- Diff_Mora - Mean_Diff
ADiff_Lead <- Diff_Lead - Mean_Diff
ADiff_Empa <- Diff_Empa - Mean_Diff
ADiff_Know <- Diff_Know - Mean_Diff
ADiff_Hard <- Diff_Hard - Mean_Diff
ADiff_Hone <- Diff_Hone - Mean_Diff
ADiff_Insp <- Diff_Insp - Mean_Diff

####################
#Comparing the Actual Differences and Bootstrapped Bounds

trait <- c("Compassionate","Moral","Strong Leader","Really Cares","Knowledgeable","Hard-Working","Honest","Inspiring")

CI95_lower <- c(quantile(MDiff_Comp,.025),quantile(MDiff_Mora,.025)
,quantile(MDiff_Lead,.025),quantile(MDiff_Empa,.025),quantile(MDiff_Know,.025),quantile(MDiff_Hard,.025),quantile(MDiff_Hone,.025),quantile(MDiff_Insp,.025))

CI95_upper <- c(quantile(MDiff_Comp,.975),quantile(MDiff_Mora,.975)
,quantile(MDiff_Lead,.975),quantile(MDiff_Empa,.975),quantile(MDiff_Know,.975),quantile(MDiff_Hard,.975),quantile(MDiff_Hone,.975),quantile(MDiff_Insp,.975))

ownership_estimate_weighted <- c(ADiff_Comp,ADiff_Mora,ADiff_Lead,ADiff_Empa,ADiff_Know,ADiff_Hard,ADiff_Hone,ADiff_Insp)

D_ratingdiff <- c(D_Comp, D_Mora, D_Lead, D_Empa, D_Know, D_Hard, D_Hone,D_Insp)

R_ratingdiff <- c(R_Comp, R_Mora, R_Lead, R_Empa, R_Know, R_Hard, R_Hone,R_Insp)

raw_Diff <- c(Diff_Comp,Diff_Mora,Diff_Lead,Diff_Empa,Diff_Know,Diff_Hard,Diff_Hone,Diff_Insp)

inductive_ownership <- data.frame(trait,ownership_estimate_weighted, CI95_lower,CI95_upper,raw_Diff,D_ratingdiff,R_ratingdiff)

write.csv(inductive_ownership,"traitreputation_weighted.csv")


